package com.jay.mypracticesdemo

import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onEach

class FlowDemo {

    class FakeDataLoadRepository {
        private val data = "FakeData"

        /**
         * 创建了一个flow 对象 去加载 fakeData
         */
        fun loadDataString(loadCount:Int): Flow<String> {
            val stringFlow = flow<String> {
                var index = 0
                while (index < loadCount) {
                    index++
                    emit(data + index)
                    delay(100)
                }
            }
            return stringFlow
                .map { it.plus(" maped") }
                .onEach { println("onEach: $it") }
        }
    }

}